package com.hs.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.dao.DaoImpl.CommunityDaoImpl;
import com.hs.dao.DaoImpl.CourierStationDaoImpl;
import com.hs.entity.Community;
import com.hs.entity.CourierStation;
import com.hs.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/courierstation")
public class CourierStationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        CourierStationDaoImpl courierStationDao = new CourierStationDaoImpl();
        if ("selectAll".equals(action)){
            //接收参数创建map集合
            String courierStationName = req.getParameter("courierStationName");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String communityName = req.getParameter("communityName");
            String courierStationStatus = req.getParameter("courierStationStatus");

            Map<String,String> map = new HashMap<>();
            map.put("courierStationName",courierStationName);
            map.put("communityName",communityName);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("courierStationStatus",courierStationStatus);
            //连接数据库

            List<CourierStation> list = courierStationDao.selectAll(map);
            int total = courierStationDao.selectCount(map);
            Map resMap = new HashMap();
            resMap.put("list",list);
            resMap.put("total",total);
            Result result = Result.success(resMap);
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("add".equals(action)) {
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCity = req.getParameter("courierStationCity");
            String id = req.getParameter("courierStationCommunityId");
            int courierStationCommunityId = 0;
            if (id!=null&& !"".equals(id)){
                courierStationCommunityId = Integer.parseInt(id);
            }
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationName = req.getParameter("courierStationName");
            String courierStationProvince = req.getParameter("courierStationProvince");
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");

            CourierStation courierStation = new CourierStation(courierStationName,courierStationProvince,courierStationCity,courierStationDistrict,courierStationAddress,courierStationCommunityId,courierStationCommunityName);
            int num = courierStationDao.insert(courierStation);
            Result result = null;
            if (num!=0){
                result = Result.success("新增成功!");
            }else {
                result = Result.error("新增失败!");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("update".equals(action)) {
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCity = req.getParameter("courierStationCity");
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationName = req.getParameter("courierStationName");
            String courierStationProvince = req.getParameter("courierStationProvince");
            String id = req.getParameter("courierStationId");
            int courierStationId = 0;
            if (id!=null&&!"".equals(id)){
                courierStationId = Integer.parseInt(id);
            }
            CourierStation courierStation = new CourierStation(courierStationId,courierStationName,courierStationProvince,courierStationCity,courierStationDistrict,courierStationAddress,courierStationCommunityName);
            int num = courierStationDao.update(courierStation);
            Result result = null;
            if (num!=0){
                result = Result.success("修改成功!");
            }else {
                result = Result.error("修改失败!");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("changeStatus".equals(action)) {
            int courierStationId = 0;
            if (req.getParameter("courierStationId")!=null){
                courierStationId = Integer.parseInt( req.getParameter("courierStationId"));
            }

            String courierStationStatus = req.getParameter("courierStationStatus");
            int num = courierStationDao.changeStatus(courierStationStatus, courierStationId);
            if (num!=0){
                Result success = Result.success("操作成功!");
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result success = Result.error("操作失败!");
                resp.getWriter().write(JSON.toJSONString(success));
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
